package xor.droid.nav;

import pb032.droid.nav.IndoorNav;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class StartingPoint extends Activity implements OnClickListener {

    private IntentIntegrator iIntegrator;

    private String qrInfo = "";

    private TextView posText;

    private Button getPosB;
    private Button startNavB;
    private Button calculateRouteB;;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	// Widescreen for AR Applications
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

	setContentView(R.layout.main);

	getPosB = (Button) findViewById(R.id.button1);
	startNavB = (Button) findViewById(R.id.button2);
	calculateRouteB = (Button) findViewById(R.id.calculateRouteB);
	posText = (TextView) findViewById(R.id.textView1);
	posText.setText("rofl");

	getPosB.setOnClickListener(this);
	startNavB.setOnClickListener(this);
	calculateRouteB.setOnClickListener(this);
    }

    public void onClick(View v) {
	switch (v.getId()) {
	case R.id.button1:
	    getQRInfo();
	    break;
	case R.id.button2:
	    Intent startNav = new Intent(StartingPoint.this, CamControl.class);
	    startNav.putExtra("pos", qrInfo);
	    startActivity(startNav);
	    break;
	case R.id.calculateRouteB:
	    startActivity(new Intent(StartingPoint.this, IndoorNav.class));
	    break;    
	}

    }

    private void getQRInfo() {
	if (iIntegrator == null)
	    iIntegrator = new IntentIntegrator(this);
	iIntegrator.initiateScan();

    }

    // Get the Results of the QR Scanner and save to the qrInfo String

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
	if (scanResult != null) {
	    qrInfo = scanResult.toString();
	    posText.setText(qrInfo);
	} else {
	    qrInfo = "Oops, something went wrong";
	}

    }

    /**
     * Android-Back-Button(non-Javadoc)
     * 
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK) {
	    this.finish();
	}
	return super.onKeyDown(keyCode, event);
    }

}
